<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/5/6  15:34
// +----------------------------------------------------------------------
namespace Admin\Controller;
class ReturnController extends CashierBaseController {

    function index() {
        $this->display();
    }
    //还书
    function doReturn(){
        $user_id=I("post.user_id");
        $order_id=I("post.order_id");
        $book_id=I("post.book_id");
        $Orderhb=M("Order_has_book");
        $map=["order_id"=>$order_id,
            "book_id"=>$book_id,
            "is_backed"=>0,
            "type"=>1];
        $d=$Orderhb->where($map)->find();
        if(!$d){
            $j=[
                "status"=>"error",
                "data"=>"此书已归还，不要重复归还"
            ];
            echo json_encode($j);
            return;
        }
        $Orderhb->startTrans();
        $Orderhb->where($map)->setInc("num_backed");
        $dd=$Orderhb->where($map)->find();
        if($dd["num_backed"]!=$d['num_backed']+1){
            $Orderhb->rollback();
            $j=[
                "status"=>"error",
                "data"=>"归还失败,"
            ];
            echo json_encode($j);
            return;
        }
        if($dd['num_backed']+$dd['num_broken']==$dd['quantity']){
            $r=$Orderhb->where($map)->data(["is_backed"=>1])->save();
            if(!$r){
                $Orderhb->rollback();
                $j=[
                    "status"=>"error",
                    "data"=>"归还失败"
                ];
                echo json_encode($j);
                return;
            }
        }
        $r=M("Book")->where(["book_id"=>$book_id])->setInc("stock_rent",1);
        if(!$r){
            $Orderhb->rollback();
            $j=[
                "status"=>"error",
                "data"=>"归还失败,,,"
            ];
            echo json_encode($j);
            return;
        }
        //记录还书记录
        $dreturn=[
            "order_id"=>$order_id,
            "book_id"=>$book_id,
            'user_id'=>$user_id
        ];
        $return_id=M('Order_book_return')->add($dreturn);
        // 统计后台用户操作还书数据使用
        $admin_order = M("admin_order");
        $admin_order->data(['admin_id' => session('admin_user.user_id'), 'order_id' => $order_id, 'type' => 1,'book_id' => $book_id])->add();
        if(!$return_id){
            $Orderhb->rollback();
            $j=[
                "status"=>"error",
                "data"=>"归还失败,无法记录还书记录"
            ];
            echo json_encode($j);
            return;
        }
        $Orderhb->commit();
        $j=[
            "status"=>"success",
            "data"=>"归还成功"
        ];
        echo json_encode($j);
        return;
    }

    //条码查询
    function getOrderBook(){
        $tm=I("post.tm");
        $user_id=I("post.user_id");
        $map=["user_id"=>$user_id,
            "tm"=>$tm,
            "is_backed"=>0,
            "type"=>1,
            "state_order"=>3
        ];
        $d=M("View_order_has_book")->where($map)->field("order_id,book_id,b_logo,dj,sm,tm,zyz,quantity,create_time,num_backed,num_broken,is_backed")->select();
        if(!$d){
            $r=[
                "status"=>"error",
                "data"=>"不存在此条形码的可还图书"
            ];
            echo json_encode($r);
            return;
        }
        $disc=C("trading_para.broken_disc"); //破损处理折扣
//        $cd=M('Card')->where(['user_id'=>$user_id])->find();
        foreach($d as $k=>$v){
            $d[$k]['dj_d']=$d[$k]['dj']*$disc; //破损书回收价格
        }
        $r=[
            "status"=>"success",
            "data"=>$d
        ];
        echo json_encode($r);
        return;
    }
}